package cn.k.service.impl;

import cn.k.domain.SystemDictionary;
import cn.k.domain.SystemMenu;
import cn.k.mapper.SystemMenuMapper;
import cn.k.qo.QueryObject;
import cn.k.qo.SystemMenuQueryObject;
import cn.k.service.ISystemMenuService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SystemMenuServiceImpl implements ISystemMenuService {
    @Autowired
    private SystemMenuMapper systemMenuMapper;

    @Override
    @Transactional
    public void insert(SystemMenu systemMenu) {
        //新增设置创建时间
        systemMenu.setCreateTime(new Date());
        //默认状态是启动
        systemMenu.setMenuStatus(1);
        systemMenuMapper.insert(systemMenu);
    }

    @Override
    public void delete(Long id) {
        systemMenuMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(SystemMenu systemMenu) {
        systemMenuMapper.updateByPrimaryKey(systemMenu);

    }

    @Override
    public SystemMenu get(Long id) {
        SystemMenu systemMenu = systemMenuMapper.selectByPrimaryKey(id);
        return systemMenu;
    }

    @Override
    public List<SystemMenu> selectAll() {
        List<SystemMenu> systemMenus = systemMenuMapper.selectAll();
        return systemMenus;
    }


    @Override
    public List<SystemMenu> selectForList(SystemMenuQueryObject qo) {
        List<SystemMenu> systemMenus = systemMenuMapper.selectForList(qo);
        return systemMenus;
    }

    @Override
    public PageInfo<SystemMenu> query(SystemMenuQueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询当前页数据
        List<SystemMenu> systemMenus = systemMenuMapper.selectForList(qo);
        return new PageInfo<>(systemMenus);
    }

    @Override
    public List<SystemMenu> queryMenu(SystemMenuQueryObject qo) {
        return systemMenuMapper.selectForMenu(qo);
    }

    @Override
    public List<SystemMenu> selectMenu(Long menuId) {
        return systemMenuMapper.selectMenu(menuId);
    }

    @Override
    public List<SystemMenu> selectMenuParent(Long id) {
        return systemMenuMapper.selectMenuParent(id);
    }

    @Override
    public List<SystemMenu> selectChildren(Integer parentId) {
        return systemMenuMapper.selectChildren(parentId);
    }

    @Override
    public List<Map> queryByMenu() {
        //拿到所有对象
        List<SystemMenu> systemMenus = systemMenuMapper.selectAll();

        ArrayList<SystemMenu> top = new ArrayList<>();
        //遍历
        for (SystemMenu menu : systemMenus) {
               if (menu.getParentId()==null){
                   top.add(menu);
                   List<SystemMenu> children = systemMenuMapper.selectMenuParent(menu.getId());//拿到所有的子类
                   menu.setChildren(children);
               }

        }
        String str = JSON.toJSONString(top);
        List<Map> maps = JSON.parseArray(str, Map.class);
        return maps;
    }

    @Override
    public void updateStatus(Long id, int status) {
         systemMenuMapper.updateStatus(id,status);
    }
}
